package com.example.myapplication.adapter;


import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import java.util.List;

public class ImageTitlePagerAdapter extends PagerAdapter {
    private List list;
    private OnImageClickListener onImageClickListener;
    public ImageTitlePagerAdapter(List list , OnImageClickListener onImageClickListener) {
        System.out.println(list.size());
        this.list = list;
        this.onImageClickListener = onImageClickListener;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = (View) list.get(position);
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent!=null){
            parent.removeView((View) list.get(position));
        }
        container.addView((View) list.get(position));
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onImageClickListener.onClick(position);
            }
        });
        return list.get(position);
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
    public interface OnImageClickListener {
        void onClick(int position);
    }
}
